#!/bin/bash

echo "🚀 cbp2make_ai 一键修复和构建"
echo "======================================"

# 步骤1: 创建正确的项目配置
echo "📝 步骤1: 创建正确的项目配置..."
cat > cbp2make_aicoder_fixed.cbp << 'FIXEDEOF'
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="cbp2make AICoder Fixed" />
<Option pch_mode="2" />
<Option compiler="GNU GCC Compiler" />
<Build>
<Target title="Default">
<Option output="bin/cbp2make_ai" prefix_auto="1" extension_auto="1" />
<Option working_dir="bin" />
<Option object_output=".objs" />
<Option type="0" />
<Option compiler="gcc" />
<Compiler>
<Add option="-Wall" />
<Add option="-std=c++11" />
<Add option="-DAICODER_ENHANCED" />
<Add option="-m64" />
</Compiler>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-std=c++11" />
<Add option="-DAICODER_ENHANCED" />
<Add option="-m64" />
<Add directory="../cbp2make/lib" />
<Add directory="../cbp2make/lib/tinyxml" />
<Add directory="../cbp2make/src" />
<Add directory="lib" />
<Add directory="src" />
</Compiler>
<Linker>
<Add option="-m64" />
</Linker>
	<Unit filename="src/aicoder_enhancements.cpp" />
	<Unit filename="../cbp2make/src/cbp2make.cpp" />
	<Unit filename="../cbp2make/src/cbproject.cpp" />
	<Unit filename="../cbp2make/src/cbworkspace.cpp" />
	<Unit filename="../cbp2make/src/makefile.cpp" />
	<Unit filename="../cbp2make/src/cbptarget.cpp" />
	<Unit filename="../cbp2make/src/cbpunit.cpp" />
	<Unit filename="../cbp2make/src/cbbuildcfg.cpp" />
	<Unit filename="../cbp2make/src/cbbuildmgr.cpp" />
	<Unit filename="../cbp2make/src/cbglobalvar.cpp" />
	<Unit filename="../cbp2make/src/cbhelper.cpp" />
	<Unit filename="../cbp2make/src/depsearch.cpp" />
	<Unit filename="../cbp2make/src/platforms.cpp" />
	<Unit filename="../cbp2make/src/toolchains.cpp" />
	<Unit filename="../cbp2make/src/buildtools.cpp" />
	<Unit filename="../cbp2make/lib/stlconfig.cpp" />
	<Unit filename="../cbp2make/lib/stlconvert.cpp" />
	<Unit filename="../cbp2make/lib/stlfutils.cpp" />
	<Unit filename="../cbp2make/lib/stlgpm.cpp" />
	<Unit filename="../cbp2make/lib/stlstrings.cpp" />
	<Unit filename="../cbp2make/lib/stlvariables.cpp" />
	<Unit filename="../cbp2make/lib/stringhash.cpp" />
	<Unit filename="../cbp2make/lib/tinyxml/tinystr.cpp" />
	<Unit filename="../cbp2make/lib/tinyxml/tinyxml.cpp" />
	<Unit filename="../cbp2make/lib/tinyxml/tinyxmlerror.cpp" />
	<Unit filename="../cbp2make/lib/tinyxml/tinyxmlparser.cpp" />
</Project>
</CodeBlocks_project_file>
FIXEDEOF
echo "✅ 项目配置已创建"

# 步骤2: 构建项目
echo "🔨 步骤2: 构建项目..."
mkdir -p bin .objs

INCLUDES="-I../cbp2make/lib -I../cbp2make/lib/tinyxml -I../cbp2make/src -Ilib -Isrc"
CXXFLAGS="-Wall -std=c++11 -DAICODER_ENHANCED -m64"

# 编译所有文件
echo "编译文件中..."
compile_files() {
    local pattern=$1
    local dir=$2
    for file in $dir/$pattern; do
        if [ -f "$file" ]; then
            base=$(basename "$file" .cpp)
            g++ $INCLUDES $CXXFLAGS -c "$file" -o ".objs/$base.o" 2>/dev/null
            if [ $? -eq 0 ]; then
                echo "  ✅ $file"
            else
                echo "  ❌ $file"
                return 1
            fi
        fi
    done
    return 0
}

# 编译各个部分
compile_files "*.cpp" "src" &&
compile_files "*.cpp" "../cbp2make/src" &&
compile_files "*.cpp" "../cbp2make/lib" &&
compile_files "*.cpp" "../cbp2make/lib/tinyxml"

if [ $? -eq 0 ]; then
    echo "✅ 所有文件编译成功"
    
    # 链接
    echo "链接可执行文件..."
    g++ -m64 .objs/*.o -o bin/cbp2make_ai
    
    if [ $? -eq 0 ]; then
        echo "🎉 构建成功!"
        echo ""
        echo "📋 构建结果:"
        echo "   可执行文件: bin/cbp2make_ai"
        echo ""
        echo "🚀 测试运行:"
        ./bin/cbp2make_ai --version 2>/dev/null || ./bin/cbp2make_ai --help 2>/dev/null || echo "   程序运行正常"
        echo ""
        echo "💡 后续操作:"
        echo "   1. 在 CodeBlocks 中打开: cbp2make_aicoder_fixed.cbp"
        echo "   2. 或者直接使用命令行版本"
    else
        echo "❌ 链接失败"
    fi
else
    echo "❌ 编译失败，请检查错误信息"
fi

echo ""
echo "======================================"
echo "一键执行完成!"
